var _util = _interopRequireDefault(require("../../../../../public/utils/util")), _api = _interopRequireDefault(require("../../../../../utils/api")), _common = require("../common");

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _slicedToArray(t, e) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, e) {
    var i = [], r = !0, a = !1, n = void 0;
    try {
        for (var s, o = t[Symbol.iterator](); !(r = (s = o.next()).done) && (i.push(s.value), 
        !e || i.length !== e); r = !0) ;
    } catch (t) {
        a = !0, n = t;
    } finally {
        try {
            r || null == o.return || o.return();
        } finally {
            if (a) throw n;
        }
    }
    return i;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var i = null != arguments[t] ? arguments[t] : {}, r = Object.keys(i);
        "function" == typeof Object.getOwnPropertySymbols && (r = r.concat(Object.getOwnPropertySymbols(i).filter(function(t) {
            return Object.getOwnPropertyDescriptor(i, t).enumerable;
        }))), r.forEach(function(t) {
            _defineProperty(e, t, i[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var LIMIT = 20, MODE = {
    time: {
        name: "最近一次访问",
        apiName: "timelists",
        formatName: "formatTimeList"
    },
    count: {
        name: "互动频率",
        apiName: "frequencylists",
        formatName: "formatCountList"
    }
}, frequencyList = [ 0, 5, 10, 30, 50, 100 ];

Component({
    properties: {
        mode: {
            type: String,
            value: "time",
            observer: function(t) {
                this.setData({
                    modeName: MODE[t].name
                });
            }
        },
        top: String
    },
    data: {
        filterId: 1,
        filterName: "全部",
        starttime: "2018-01-01",
        endtime: _util.default.formatLocalDate(new Date(), "Y-M-D"),
        skeleton: !0,
        visitorUser: {},
        modeName: MODE.time.name,
        count: 0,
        frequency: 0,
        lists: [ Array(8).fill({
            realname: "**",
            lastview: "**"
        }) ],
        frequencyList: frequencyList.map(function(t) {
            return t ? t + "次以上" : "全部";
        })
    },
    created: function() {
        this.noMore = {}, this.offset = 0;
    },
    methods: {
        init: function(t) {
            this.data.skeleton && this.selectComponent("#skeleton2").create(this), this.loadList(!0, t);
        },
        onPullDownRefresh: function() {
            this.loadList(!0, wx.stopPullDownRefresh);
        },
        onReachBottom: function() {
            this.noMore || this.loadList(!1);
        },
        onFilter: function() {
            "time" === this.data.mode && this.selectComponent("#timeFilter").show();
        },
        frequencyFilter: function(t) {
            var e = this, i = frequencyList[t.detail.value];
            this.setData({
                frequency: i,
                filterId: i ? 0 : 1
            }, function() {
                return e.loadList(!0);
            });
        },
        onReset: function(t) {
            var e = this, i = t.detail, r = i.start, a = i.end, n = i.name, s = i.id;
            this.setData({
                starttime: r,
                endtime: a,
                filterName: n,
                filterId: s
            }, function() {
                return e.loadList(!0);
            });
        },
        onConfirm: function(t) {
            var e = this, i = t.detail, r = i.start, a = i.end, n = i.name, s = void 0 === n ? "" : n, o = i.id, l = void 0 === o ? "" : o;
            this.setData({
                starttime: r,
                endtime: a,
                filterName: s,
                filterId: l
            }, function() {
                return e.loadList(!0);
            });
        },
        loadList: function(a, t) {
            var n = this;
            a && (this.offset = 0);
            var e = this.data, s = e.mode, i = e.frequency, r = e.starttime, o = e.endtime, l = "time" === s ? {
                starttime: r,
                endtime: o
            } : {
                frequency: i
            };
            _api.default.market.log[MODE[s].apiName]({
                data: _objectSpread({
                    limit: LIMIT,
                    offset: this.offset
                }, l),
                complete: t
            }).then(function(t) {
                var e = t.data, i = e.lists, r = e.count;
                i = n[MODE[s].formatName](i), n.handleList(i, r, a);
            });
        },
        handleList: function(t, e, i) {
            if (i) this.setData({
                count: e,
                lists: [ t ],
                skeleton: !1
            }); else {
                var r = (this.data.lists || []).length;
                this.setData(_defineProperty({}, "lists[".concat(r, "]"), t));
            }
            this.offset += LIMIT, this.noMore = t.length < LIMIT;
        },
        formatTimeList: function(t) {
            return t.map(function(t) {
                t.realname = t.desc || t.realname;
                var e = _slicedToArray(_util.default.formatLocalDate(t.createtime, "Y年M月D日 h").split(" "), 2), i = e[0], r = e[1];
                return t.showData = i + _util.default.getTimeToLocal(r), t;
            });
        },
        formatCountList: function(t) {
            return t.map(function(t) {
                return t.showData = "累计".concat(t.num, "次"), t;
            });
        },
        gotoChat: function(t) {
            if ("card" === this.data.mode && !this.$appData.card.isFeeVer) return this.gotoRadarIndex();
            (0, _common.gochat)(t, this);
        },
        gotoRadarIndex: function() {
            this.$route("/pages/radar/index/index", "tab");
        },
        showUserLists: function(t) {
            var e = t.currentTarget.dataset, i = e.index, r = e.childindex, a = this.data.lists[i][r];
            this.setData({
                visitorUser: _objectSpread({}, a, {
                    isOpen: !0
                })
            }), this.loadUserList(!0), this.toggleTabBar(!0);
        },
        hideUserLists: function() {
            this.setData({
                visitorUser: {
                    isOpen: !1
                }
            }), this.toggleTabBar(!1);
        },
        toggleTabBar: function(t) {
            t ? wx.hideTabBar({
                animation: !0
            }) : wx.showTabBar();
        },
        loadMoreUserList: function() {
            this.userNoMore || this.loadUserList();
        },
        loadUserList: function(i) {
            var r = this;
            i && (this.userOffset = 0);
            var t = this.data.visitorUser;
            _api.default.market.log.lists({
                data: {
                    limit: LIMIT,
                    fromuid: t.uid,
                    offset: this.userOffset
                }
            }).then(function(t) {
                var e = t.data.lists;
                return r.handleUserList(e, i);
            });
        },
        handleUserList: function(t, e) {
            var i = (this.data.visitorUser || {
                lists: []
            }).lists;
            t = (0, _common.handleVisitorLists)(t), e ? this.setData({
                "visitorUser.lists": [ t ],
                visitorUserSkeleton: !1
            }) : this.setData(_defineProperty({}, "visitorUser.lists[".concat(i.length, "]"), t)), 
            this.userOffset += LIMIT, this.userNoMore = t.length < LIMIT;
        }
    }
});